﻿using System.Collections.Generic;
using System.Linq;
using Moonwish.Core;
using Moonwish.Core.ModelBase;
using Moonwish.Foundation.DataEntity;

namespace Moonwish.Foundation.ValueObjects
{
    /// <summary>
    /// 值对象ViewModel，by zhlin 2011-08-25
    /// </summary>
    public class ValueObjectModel : ModelBase
    {
        private readonly ValueObjectServiceClient _client = new ValueObjectServiceClient();
        private readonly ValueObjectClassCollection _valueObjectClasses;
        internal readonly Dictionary<string,ValueObjectClass> QuicklyClasses = new Dictionary<string, ValueObjectClass>();
        internal readonly Cache<V_FDT_ValueObjectClass> CacheVoClass = new Cache<V_FDT_ValueObjectClass>();
        public const string QueryIdentityClass = "QueryValueObjectClass";

        public ValueObjectModel()
        {
            _valueObjectClasses = new ValueObjectClassCollection(this);
        }

        public override void QueryAsync(Request request)
        {
            if (request.What == "QueryValueObjectClass")
            {
                var cacheKey = request.GetHashCode();
                CacheVoClass.CurrentKey = cacheKey;
                if (CacheVoClass.ContainsKey(cacheKey))
                {
                    PushVoBusinessData();
                }
                else
                {
                    _client.QueryValueObjectClassCompleted += QueryValueObjectClassCompleted;
                    _client.QueryValueObjectClassAsync(request);
                }
            }
        }

        void QueryValueObjectClassCompleted(object sender, QueryValueObjectClassCompletedEventArgs e)
        {
            try
            {
                if (e.Error != null)
                {
                    // 调用完成事件。
                    InvokeQueryCompleted(sender, e);

                    return;
                }

                // 缓存实体数据
                CacheVoClass[CacheVoClass.CurrentKey] = e.Result;

                // 组装业务数据
                PushVoBusinessData();
            }
            finally 
            {
                _client.QueryValueObjectClassCompleted -= QueryValueObjectClassCompleted;
            }
        }

        /// <summary>
        /// 组装业务数据。
        /// </summary>
        void PushVoBusinessData()
        {
            QuicklyClasses.Clear();
            ValueObjectClasses.Clear();

            var data = CacheVoClass[CacheVoClass.CurrentKey].OrderBy(obj => obj.Name);

            foreach (var item in data)
            {
                var classObj = new ValueObjectClass(item);

                classObj.Model = this;

                _valueObjectClasses.BaseAdd(classObj);

                QuicklyClasses[classObj.Id] = classObj;

                foreach (var valueObject in item.V_FDT_ValueObjects.OrderBy(obj => obj.OrderNo))
                {
                    var valueObj = new ValueObject(valueObject) {Model = this};
                    classObj.ValueObjects.BaseAdd(valueObj);
                    classObj.QuicklyValueObjects[valueObj.Id] = valueObj;
                }
            }

            // 调用完成事件。
            InvokeQueryCompleted();
        }

        internal ValueObjectServiceClient ValueObjectProxy
        {
            get
            {
                return _client;
            }
        }

        public ValueObjectClassCollection ValueObjectClasses
        {
            get
            {
                return _valueObjectClasses;
            }
        }
    }
}
